from time import sleep,time
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as ec
from selenium.webdriver.remote.webelement import WebElement

TIMEOUT = 8
class Base():
    # 初始化驱动
    def __init__(self,driver):
        #判断
        self.driver=driver


    #打开网址方法
    def goto(self,url):
        self.driver.get(url)

    # 显示等待元素点击
    # def wait_click(self, loc, timeout=None) -> WebElement:
    #     timeout = TIMEOUT if timeout is None else timeout
    #     wait = WebDriverWait(self.driver, timeout=timeout)
    #     return wait.until(ec.element_to_be_clickable(loc))

    def wait_click(self,loc,timeout=None)->WebElement:
        timeout=TIMEOUT if timeout is None else timeout
        wait=WebDriverWait(self.driver,timeout=timeout)
        return wait.until(ec.element_to_be_clickable(loc))

    # 显示等待文本
    def wait_text(self, loc, timeout=None) -> WebElement:
        timeout = TIMEOUT if timeout is None else timeout
        wait = WebDriverWait(self.driver, timeout=timeout)
        return wait.until(ec.visibility_of_element_located(loc))
    # 封装定位方法
    def find_element(self,loc):
       return self.driver.find_element(*loc)
    # 点击事件
    def click(self,loc):
        self.wait_click(loc).click()
    # 输入框输入事件
    def send_keys(self,loc,info):
        self.wait_click(loc).send_keys(info)
    #封装清空
    def clear(self,loc):
        self.wait_click(loc).clear()
    def quit(self):
        self.driver.quit()
    #封装text()文本断言
    def text(self,loc):
        return self.wait_text(loc).text
    #封装强制等待
    def sleep(self, many):
        sleep(int(many))
